package system;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

// 游戏逻辑类，处理游戏的核心逻辑
public class GameLogic {
    private static final int MAX_AGE = 100;
    private static final int MIN_WORK_AGE = 18;
    private static final int HEALTH_BOTTOM_LINE = 20;
    private static final int WEALTH_BOTTOM_LINE = 0;
    private static final int HAPPINESS_BOTTOM_LINE = 10;
    private static final int HAPPINESS_TOP_LINE = 80;
    private static final String[] FIRST_NAMES = {"赵", "钱", "孙", "李", "周", "吴", "郑", "王"};
    private static final String[] LAST_NAMES = {"伟", "芳", "娜", "敏", "静", "强", "磊", "军"};

    private int age;
    private int health;
    private int wealth;
    private int happiness;
    private boolean isMarried;
    private int childrenCount;
    private boolean isEmployed;
    private String name;
    private String spouseName;
    private List<String> childrenNames;
    private Random random;

    public GameLogic() {
        this.age = 0;
        this.health = 100;
        this.wealth = 0;
        this.happiness = 50;
        this.isMarried = false;
        this.childrenCount = 0;
        this.isEmployed = false;
        this.random = new Random();
        this.name = generateRandomName();
        this.childrenNames = new ArrayList<>();
    }

    // 生成随机名字
    private String generateRandomName() {
        String firstName = FIRST_NAMES[random.nextInt(FIRST_NAMES.length)];
        String lastName = LAST_NAMES[random.nextInt(LAST_NAMES.length)];
        return firstName + lastName;
    }

    // 学习方法
    public String study() {
        StringBuilder result = new StringBuilder("你开始努力学习。\n");
        int healthBefore = health;
        int wealthBefore = wealth;
        int happinessBefore = happiness;
        health -= 10;
        wealth -= 20;
        happiness += 10;
        if (random.nextInt(100) < 50) {
            result.append("学习取得了好成果，为未来的工作打下了基础。\n");
        } else {
            result.append("学习效果一般，继续加油。\n");
        }
        result.append(String.format("学习后，健康从 %d 变为 %d，财富从 %d 变为 %d，幸福度从 %d 变为 %d。\n",
                healthBefore, health, wealthBefore, wealth, happinessBefore, happiness));
        result.append(randomEvent());
        result.append(checkBottomLines());
        age++;
        return result.toString();
    }

    // 工作方法
    public String work() {
        StringBuilder result = new StringBuilder();
        if (age < MIN_WORK_AGE) {
            result.append("你还未到工作年龄，先好好学习吧。\n");
            age++;
            result.append(randomEvent());
            result.append(checkBottomLines());
            return result.toString();
        }
        if (!isEmployed) {
            result.append("你处于失业状态，无法工作。先努力找工作吧。\n");
            age++;
            result.append(randomEvent());
            result.append(checkBottomLines());
            return result.toString();
        }
        result.append("你开始工作。\n");
        int healthBefore = health;
        int wealthBefore = wealth;
        int happinessBefore = happiness;
        health -= 20;
        wealth += 50;
        happiness -= 10;
        if (random.nextInt(100) < 30) {
            result.append("工作中遇到了困难，心情有些低落。\n");
        } else {
            result.append("工作进展顺利，获得了一笔收入。\n");
        }
        result.append(String.format("工作后，健康从 %d 变为 %d，财富从 %d 变为 %d，幸福度从 %d 变为 %d。\n",
                healthBefore, health, wealthBefore, wealth, happinessBefore, happiness));
        result.append(randomEvent());
        result.append(checkBottomLines());
        age++;
        return result.toString();
    }

    // 休息方法
    public String rest() {
        StringBuilder result = new StringBuilder("你选择休息。\n");
        int healthBefore = health;
        int wealthBefore = wealth;
        int happinessBefore = happiness;
        health += 30;
        wealth -= 10;
        happiness += 20;
        result.append("休息后感觉精神焕发。\n");
        result.append(String.format("休息后，健康从 %d 变为 %d，财富从 %d 变为 %d，幸福度从 %d 变为 %d。\n",
                healthBefore, health, wealthBefore, wealth, happinessBefore, happiness));
        result.append(randomEvent());
        result.append(checkBottomLines());
        age++;
        return result.toString();
    }

    // 寻找伴侣方法
    public String findPartner() {
        StringBuilder result = new StringBuilder("你开始寻找伴侣。\n");
        if (isMarried) {
            result.append("你已经结婚了，不能再寻找伴侣。\n");
            age++;
            result.append(randomEvent());
            result.append(checkBottomLines());
            return result.toString();
        }
        int happinessBefore = happiness;
        if (random.nextInt(100) < 50) {
            spouseName = generateRandomName();
            result.append("你找到了合适的伴侣，名为 " + spouseName + "，步入了婚姻的殿堂。\n");
            isMarried = true;
            happiness += 30;
        } else {
            result.append("这次没有找到合适的伴侣，继续加油。\n");
        }
        result.append(String.format("寻找伴侣后，幸福度从 %d 变为 %d。\n", happinessBefore, happiness));
        result.append(randomEvent());
        result.append(checkBottomLines());
        age++;
        return result.toString();
    }

    // 随机事件方法
    private String randomEvent() {
        StringBuilder eventResult = new StringBuilder();
        // 简单示例：生病事件
        if (random.nextInt(100) < 10) {
            int healthBefore = health;
            int wealthBefore = wealth;
            int happinessBefore = happiness;
            int healthLoss = 20;
            int wealthLoss = 30;
            int happinessLoss = 10;
            health -= healthLoss;
            wealth -= wealthLoss;
            happiness -= happinessLoss;
            eventResult.append(String.format("你生病了，健康从 %d 减少到 %d，财富因医疗费用从 %d 减少到 %d，幸福度从 %d 减少到 %d。\n",
                    healthBefore, health, wealthBefore, wealth, happinessBefore, happiness));
        }
        // 生子事件
        if (isMarried && random.nextInt(100) < 5) {
            String childName = generateRandomName();
            childrenNames.add(childName);
            childrenCount++;
            happiness += 20;
            wealth -= 50;
            eventResult.append("你的伴侣生下了一个孩子，名为 " + childName + "。幸福度增加 20，财富减少 50。\n");
        }
        return eventResult.toString();
    }

    // 检查底线触发事件
    private String checkBottomLines() {
        StringBuilder result = new StringBuilder();
        if (health <= HEALTH_BOTTOM_LINE) {
            result.append("你的健康状况很差，需要好好调养。\n");
        }
        if (wealth <= WEALTH_BOTTOM_LINE) {
            result.append("你身无分文，生活陷入困境。\n");
        }
        if (happiness <= HAPPINESS_BOTTOM_LINE) {
            result.append("你心情极度低落，伤心地去喝酒了。\n");
            if (random.nextInt(100) < 20) {
                result.append("你因过度伤心自我毁灭了，游戏结束。\n");
                // 这里可以添加游戏结束逻辑
            }
        }
        if (happiness >= HAPPINESS_TOP_LINE) {
            result.append("你心情非常开心，举办了一场聚会。\n");
        }
        return result.toString();
    }

    // 获取当前年龄
    public int getAge() {
        return age;
    }

    // 获取当前健康值
    public int getHealth() {
        return health;
    }

    // 获取当前财富值
    public int getWealth() {
        return wealth;
    }

    // 获取当前幸福度
    public int getHappiness() {
        return happiness;
    }

    // 获取婚姻状态
    public boolean isMarried() {
        return isMarried;
    }

    // 获取子女数量
    public int getChildrenCount() {
        return childrenCount;
    }

    // 获取就业状态
    public boolean isEmployed() {
        return isEmployed;
    }

    // 获取本人名字
    public String getName() {
        return name;
    }

    // 获取配偶名字
    public String getSpouseName() {
        return spouseName;
    }

    // 获取子女名字列表
    public List<String> getChildrenNames() {
        return childrenNames;
    }
}